﻿@Master['Master'] @Section['Content']
<script>
$(function(){
//$('#sendmail-btn').click(function() {
$('#sendmail-btn').on('click', function(){
//getAllValues();
console.log(buildRequestStringData($('#mailinput')));
var maildata = JSON.parse(buildRequestStringData($('#mailinput')));
//console.log(buildRequestStringData($('#mailinput')));

//console.log(maildata);

$.post("maintenance/sendmail", maildata, function( data ){ })
   .error(function() {});
        //$(this).find('.btn').toggleClass('btn-default');
    });
});


function getAllValues() {
  var allVal = '';
  $("#mailinput input").each(function() {
    allVal += '"'+ $(this).attr('name') + '":"' + $(this).val() + '",';
  });
   $("#mailinput textarea").each(function() {
    allVal += '"'+ $(this).attr('name') + '":"' + $(this).val().replace(/\n\r?/g, '\\n\\r'); + '",';
  });
   requestString = allVal;//.substring(0, allVal.length - 1);
  return '{' +requestString+ '}';
}

function buildRequestStringData(form) {
    var select = form.find('select'),
        input = form.find('input'),
		textarea = form.find('textarea'),
        requestString = '{';
	
	//select
    for (var i = 0; i < select.length; i++) {
        requestString += '"' + $(select[i]).attr('name') + '": "' +$(select[i]).val() + '",';
    }
    if (select.length > 0) {
        requestString = requestString.substring(0, requestString.length - 1);
    }
	
	//input
    for (var i = 0; i < input.length; i++) {
        if ($(input[i]).attr('type') !== 'checkbox') {
            requestString += '"' + $(input[i]).attr('name') + '":"' + $(input[i]).val() + '",';
        } else {
            if ($(input[i]).attr('checked')) {
                requestString += '"' + $(input[i]).attr('name') + '":"' + $(input[i]).val() +'",';
            }
        }
    }
    if (input.length > 0) {
        //requestString = requestString.substring(0, requestString.length - 1);
    }
	
	//textarea
	  for (var i = 0; i < textarea.length; i++) {
        requestString += '"' + $(textarea[i]).attr('name') + '":"' + $(textarea[i]).val().replace(/\n\r?/g, '\\n\\r') + '",';
    }
    if (textarea.length > 0) {
        requestString = requestString.substring(0, requestString.length - 1);
    }
	
    requestString += '}';
    return requestString;
}

$(function(){
    $('.btn-toggle').click(function() {
	var name;
        $(this).find('.btn').toggleClass('active');
        if ($(this).find('.btn-primary').size() > 0) {
            $(this).find('.btn').toggleClass('btn-primary');
			 name = ($(this).find('.btn-primary').data("name"));
			var l = Ladda.create($(this).find('.btn-primary').get( 0 ));
			l.start();
        }
		$.post("maintenance/maintupdate", {action : name }, function( data ){
 l.stop();
 $.gritter.add({
                            title: 'Server Update',
                            text: data,
                            sticky: false,
                            time: ''
                     });
 })
   .error(function() {l.stop(); 
   });
        $(this).find('.btn').toggleClass('btn-default');
    });
});
$(function(){
   $('.maint').click(function(){
   var name = (this.name);
   var text = $(this).find('span').text();
   var $maint = this;
   var l = Ladda.create(this);
	l.start();
if (typeof $(this).data('confirm') !== 'undefined')
{
        if (!$('#dataConfirmModal').length) {
			$('#confirm').append('<div id="dataConfirmModal" class="modal fade" role="dialog" aria-labelledby="dataConfirmLabel" aria-hidden="true">            <div class="modal-dialog modal-md">              <div class="modal-content">                  <div class="modal-header">                      <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>                      <h4 id="dataConfirmLabel" class="modal-title"></h4>                  </div>                  <div class="modal-body">...</div><div class="modal-footer"><button class="btn" id="dataConfirmCancel" data-dismiss="modal" aria-hidden="true">Cancel</button>                        <button data-dismiss="modal" class="btn btn-primary" id="dataConfirmOK">OK</button></div>              </div>            </div>        </div>');
        } 
        $('#dataConfirmModal').find('.modal-body').text($(this).attr('data-confirm'));
		$('#dataConfirmModal').find('#dataConfirmLabel').text($(this).attr('data-msg'));
        $('#dataConfirmModal').modal({show:true});
$('#dataConfirmOK').click(function() {
    // handle deletion here
  	//var id = $('#myModal').data('id');
  	//$('[data-id='+id+']').remove();
	//action(name);
	if (name !==null)
{	
	//console.info(name);
	action(name);
	}
});
$('#dataConfirmCancel').click(function() {
    // handle Cancel here
 l.stop();
     name = null;
    text = null;
    $maint = null;
 return;
});
$('#dataConfirmModal').on('hidden.bs.modal', function () {
     l.stop();
     name = null;
    text = null;
    $maint = null;
	 return;
})
} else {
if (typeof name !== 'undefined')
{
action(name);
}
}
function action(name)
{
$.post("maintenance/maintupdate", {action : name }, function( data ){
 l.stop();
if (name == "datefix" && data.length > 0){
$.gritter.add({
                            title: 'Database Update',
                            text: 'The system updated ' + data + ' records' + '.',
                            sticky: false,
                            time: ''
                     });
}
if (name == "queuecountfix" && data.length > 0){
$.gritter.add({
                            title: 'Database Update',
                            text: 'The system updated ' + data + ' records' + '.',
                            sticky: false,
                            time: ''
                     });
}
if (name == "restart" && data.length > 0){
$.gritter.add({
                            title: 'System Update',
                            text: 'The system restart ' + data + '.',
							class_name: 'growl-info',
                            sticky: false,
                            time: ''
                     });
}
if (name == "vacuum" && data.length > 0){
$.gritter.add({
                            title: 'Database Update',
                            text: 'Database Compression completed '+ data +' were saved.',
                            sticky: false,
                            time: ''
                     });
$($maint).find('.badge').text("@Model.SQLSize");
}
if (name == "backup" && data.length > 0){
$.gritter.add({
                            title: 'Database Backup',
                            text: data,
                            sticky: false,
                            time: ''
                     });
}
if (name == "cleanlogs" && data.length > 0){
$.gritter.add({
                            title: 'Log Files',
                            text: data,
                            sticky: false,
                            time: ''
                     });
}
if (name == "cleaneml" && data.length > 0){
$.gritter.add({
                            title: 'EML Files',
                            text: 'Email clean up completed @Model.TotalEML files deleted.',
                            sticky: false,
                            time: ''
                     });
	$($maint).find('.badge').text("0");
}
if (name == "updatecheck"){
$.gritter.add({
                            title: 'Update Check',
                            text: 'System started an update check.',
							class_name: 'growl-info',
                            sticky: false,
                            time: ''
                     });
}
if (name == "clearmail"){
$($maint).find('.badge').text("0");
}
})
   .error(function( data ) {
   l.stop(); 
if (name == "updatecheck"){
$.gritter.add({
                            title: 'Update Check',
                            text: 'Update was perfromed, check the logs for details.',
							class_name: 'growl-info',
                            sticky: false,
                            time: ''
                     });
return;
}
 $.gritter.add({
                            title: 'Error notice!',
                            text: 'There was an unknow error with ' + name + '.',
                            sticky: false,
                            time: ''
                     });
   });
   }
});
});
            jQuery(document).ready(function() {
              jQuery('#growl1').click(function(){
                     jQuery.gritter.add({
                            // (string | mandatory) the heading of the notification
                            title: 'This is a regular notice!',
                            // (string | mandatory) the text inside the notification
                            text: 'This will fade out after a certain amount of time.',
                            // (string | optional) the image to display on the left
                            image: 'images/screen.png',
                            // (bool | optional) if you want it to fade out on its own or just sit there
                            sticky: false,
                            // (int | optional) the time you want it to be alive for before fading out
                            time: ''
                     });
                     return false;
              });
              jQuery('#growl2').click(function(){
                     jQuery.gritter.add({
                            title: 'This is a regular notice!',
                            text: 'This will fade out after a certain amount of time.',
                            sticky: false,
                            time: ''
                     });
                     return false;
              });
              jQuery('#growl-primary').click(function(){
                     jQuery.gritter.add({
                            title: 'This is a regular notice!',
                            text: 'This will fade out after a certain amount of time.',
                  class_name: 'growl-primary',
                  image: 'images/screen.png',
                            sticky: false,
                            time: ''
                     });
                     return false;
              });
              jQuery('#growl-success').click(function(){
                     jQuery.gritter.add({
                            title: 'This is a regular notice!',
                            text: 'This will fade out after a certain amount of time.',
                  class_name: 'growl-success',
                  image: 'images/screen.png',
                            sticky: false,
                            time: ''
                     });
                     return false;
              });
              jQuery('#growl-warning').click(function(){
                     jQuery.gritter.add({
                            title: 'This is a regular notice!',
                            text: 'This will fade out after a certain amount of time.',
                  class_name: 'growl-warning',
                  image: 'images/screen.png',
                            sticky: false,
                            time: ''
                     });
                     return false;
              });
              jQuery('#growl-danger').click(function(){
                     jQuery.gritter.add({
                            title: 'This is a regular notice!',
                            text: 'This will fade out after a certain amount of time.',
                  class_name: 'growl-danger',
                  image: 'images/screen.png',
                            sticky: false,
                            time: ''
                     });
                     return false;
              });
              jQuery('#growl-info').click(function(){
                     jQuery.gritter.add({
                            title: 'This is a regular notice!',
                            text: 'This will fade out after a certain amount of time.',
                  class_name: 'growl-info',
                  image: 'images/screen.png',
                            sticky: false,
                            time: ''
                     });
                     return false;
              });
            });
</script>
<div class="container">
	<div class="btn-group btn-toggle tooltips" data-placement="top" data-style="expand-right" data-toggle="tooltip" data-original-title="Start / Stop STMP service">
		<button class="btn btn-md @If.SmtpRunning btn-primary active @EndIf @IfNot.SmtpRunning btn-default @EndIf ladda-button" data-name="start" data-size="l" data-style="expand-right"><i class="fa fa-power-off hide"></i> Start <span class="ladda-label"></span></button>
		<button class="btn btn-md @If.SmtpRunning btn-default @EndIf @IfNot.SmtpRunning btn-primary active @EndIf ladda-button" data-name="stop" data-size="l" data-style="expand-right"><i class="fa fa-power-off hide"></i> Stop <span class="ladda-label"></span></button>
	</div>
	<div class="btn-group">
		<a class="maint btn navbar-btn btn-success ladda-button" href "#" #" data-size="l" data-style="expand-right" name="restart"><i class="fa fa-power-off"></i> Restart Webserver <span class="ladda-label"></span></a>
	</div>
	<div class="row">
		<div class="form-group col-md-3">
			<button class="maint btn btn-warning form-control ladda-button tooltips" data-msg="Clear Messages" data-confirm="Are you sure you want to delete all mail messages?" data-placement="top" data-toggle="tooltip" data-original-title="Clear all received email messages" data-size="l" data-style="expand-right" name="clearmail">Clear Mail <span class="badge ladda-label"> @Model.MailCount</span>
			</button>
		</div>
		<div class="form-group col-md-3">
			<button class="maint btn btn-warning form-control ladda-button tooltips" data-toggle="modal" data-msg="Clear Logs" data-confirm="Clear all log messages?" data-placement="top" data-toggle="tooltip" data-original-title="Clear all log messages" data-size="l" data-style="expand-right" name="cleanlogs">Clear Logs <span class="badge"> @Model.AllLogs</span><span class="ladda-label"></span>
			</button>
		</div>
		<div class="form-group col-md-3 .col-md-offset-3">
			<a class="maint btn btn-warning form-control ladda-button tooltips" data-placement="top" data-toggle="tooltip" data-original-title="Compress or Vacuum Database" data-size="l" data-style="expand-right" name="vacuum">Compress Database <span class="badge"> @Model.SQLSize</span><span class="ladda-label"></span></a>
			</button>
		</div>
	</div>
	<div class="row">
		<div class="form-group col-md-3">
			<div>
				<a class="maint btn btn-warning form-control ladda-buttontooltips" data-placement="top" data-toggle="tooltip" data-original-title="Custom Query Data" " data-size="l" data-style="expand-right" name="query" data-query="SELECT date(receive_date) FROM emaillog;">Query Database <span class="badge"></span><span class="ladda-label"></span></a>
			</div>
		</div>
		<div class="form-group col-md-3">
			<a class="maint btn btn-warning form-control ladda-button tooltips" data-placement="top" data-msg="Clean EML files" data-confirm="Are you sure you want to delete all EML files?" data-toggle="tooltip" data-original-title="Delete All EML files" data-size="l" data-style="expand-right" name="cleaneml">Clean EML Files <span class="badge ladda-label"> @Model.TotalEML</span></a>
			</button>
		</div>
		<div class="form-group col-md-3">
			<a class="maint btn btn-info form-control ladda-button tooltips" data-placement="top" data-toggle="tooltip" data-original-title="Backup Own Database" data-size="l" data-style="expand-right" name="backup" href="#">Backup Database <span class="badge"></span><span class="ladda-label"></span></a>
		</div>
	</div>
<div class="row">
	<div class="form-group col-md-3">
		<a data-target=".bs-composemail" data-toggle="modal" href="#" class="maint btn btn-info form-control ladda-button tooltips" data-placement="top" data-size="l" data-style="expand-right" data-toggle="tooltip" data-original-title="Compose an email message">Compose email <span class="badge ladda-label"></span></a>
	</div>
	<!--<div class="form-group col-md-3">
		<a class="maint btn btn-info form-control ladda-button tooltips" data-placement="top" data-toggle="tooltip" data-msg="Update DB Timestamps" data-confirm="Are you sure you want to update all timestamps?" data-original-title="Corrects DateTime in email table" data-size="l" data-style="expand-right" name="datefix" href="#">Time Update <span class="badge"></span><span class="ladda-label"></span></a>
	</div>
	<div class="form-group col-md-3">
		<a class="maint btn btn-info form-control ladda-button tooltips" data-placement="top" data-toggle="tooltip" data-msg="Update DB QueryCount" data-confirm="Add Query Count to all unsent email" data-original-title="Adds Query Count to all unsent email" data-size="l" data-style="expand-right" name="queuecountfix" href="#">QueryCount Update <span class="badge"></span><span class="ladda-label"></span></a>
	</div>  -->
	<div class="form-group col-md-3">
		<button class="maint btn btn-info form-control ladda-button tooltips" data-placement="top" data-toggle="tooltip" data-original-title="Check for OwnSmtp updates" data-size="l" data-style="expand-right" name="updatecheck">Check for Updates <span class="badge ladda-label"></span></button>
	</div>
		<div class="form-group col-md-3">
		<a id="testmail" class="maint btn btn-info form-control ladda-button tooltips" data-placement="top" data-toggle="tooltip" data-original-title="Send test email" data-size="l" data-style="expand-right" name="testmail" href="#">Test email <span class="badge ladda-label"></span></a>
	</div>
	</div>
</div>
<div class="modal fade bs-composemail" tabindex="-1" role="dialog">
	<div class="modal-dialog modal-lg">
		<div class="modal-content">
			<div class="modal-header">
				<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
				<h4 class="modal-title">Compose Email</h4>
			</div>
			<div id="mailinput" class="modal-body">
				<div>
					<div class="panel panel-default">
						<div class="row">
							<div class="form-group col-md-4">
								<input type="text" class="form-control" placeholder="Name" name="name">
							</div>
							<div class="form-group col-md-4">
								<input type="from" class="form-control" placeholder="From" name="from">
							</div>
							<div class="form-group col-md-4">
								<input type="url" class="form-control" placeholder="To" name="to">
							</div>
						</div>
						<!-- row -->
						<div class="row">
							<div class="form-group col-md-4">
								<input type="url" class="form-control" placeholder="Subject" name="subject">
							</div>
						</div>
						<textarea type="text" placeholder="Message" rows="5" class="form-control" name="message"></textarea>
					</div>
					<!-- panel -->
				</div>
				<button id="sendmail-btn" class="btn btn-primary">Send Message</button>
			</div>
		</div>
	</div>
</div>
<div id="confirm">
</div>
@EndSection